/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.owners;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import com.pet.PetinfoBean;

/**
 *
 * @author Administrator
 */
public class aServlet extends HttpServlet {


     public void wrong1(){    //对话框提示信息
        String msg="不能为空，请输入名字！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
     public void wrong2(){    //对话框提示信息
        String msg="此姓名不存在，无法修改！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    public void right(){    //对话框提示信息
        String msg="查询成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="提示信息";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
         String oname=request.getParameter("name");
         System.out.println(oname);
         if(oname.length()==0){
             wrong1();
              response.sendRedirect("http://localhost:8084/jspsx/owner/queryowner.jsp");
             
         }else{
              try{
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/abin? useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","admin");
                stmt=con.createStatement();
                String sql1="select * from t_owners,t_pets where oname='"+oname+"' and owner_name='"+oname+"'";
                rs=stmt.executeQuery(sql1);
                rs.last();
                int k=rs.getRow();
                rs.beforeFirst();
                if(k<1){
                    wrong2();
                    response.sendRedirect("http://localhost:8084/jspsx/owner/queryowner.jsp");
                }else{
                    HttpSession session=request.getSession();
                    ArrayList petlist =null;
                    petlist=new ArrayList();
//                    HttpSession session=request.getSession();
                    ArrayList ownerlist=null;
                    ownerlist=new ArrayList();
                    while(rs.next()){
                        OwnersBean ff=new OwnersBean();
                        PetinfoBean dd=new PetinfoBean();
                        ff.setId(rs.getString("id"));
                        ff.setOname(rs.getString("oname"));
                        ff.setAddress(rs.getString("address"));
                        ff.setCity(rs.getString("city"));
                        ff.setTelephone(rs.getString("telephone"));
                        ff.setPetname(rs.getString("name"));
                        dd.setId(rs.getString("petid"));
                        dd.setPetname(rs.getString("name"));
                        dd.setBirthdate(rs.getString("birth_date"));
                        dd.setType(rs.getString("type"));
                        dd.setOwnerid(rs.getString("owner_name"));
                        ownerlist.add(ff);
                        session.setAttribute("ownerlist",ownerlist);
                        petlist.add(dd);
                        session.setAttribute("petlist",petlist);
                        right();
                        response.sendRedirect("http://localhost:8084/jspsxxxx/luru/chanxunzhanhsi.jsp"); 
                    } 
                }
                //实例化保存个人信息的JavaBean
                ArrayList ownerlist1 =null;
                    ownerlist1=new ArrayList();
                    HttpSession session=request.getSession();
               PetinfoBean nn=new PetinfoBean();
               nn.setPetname(oname);
               ownerlist1.add(nn);
               session.setAttribute("ownerlist1",ownerlist1);  
                             rs.close();
                             stmt.close();
                             con.close();
//                             right();
//                             response.sendRedirect("http://localhost:8084/jspsxxxx/luru/chanxunzhanhsi.jsp");   
            }catch(Exception e){
                e.printStackTrace();
            }   
         }   
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
